package com.home.base.util;

import com.gargoylesoftware.htmlunit.html.HtmlInput;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author ：chenxf
 * @date ：Created in 2019/8/21 16:39
 * @description：
 * @modified By：
 * @version: $
 */
public class SeleniumUtil {

    String baseUri="https://www.maersk.com.cn/";

    public void loginYY( String username,String password){
       // String pathUri="C:\\Users\\hcyx\\AppData\\Local\\Google\\Chrome\\User Data";
        String userDir = System.getProperty("user.dir");
        String pathUri=userDir+"\\doc\\User Data";

        System.setProperty("webdriver.chrome.driver", userDir+"//doc//chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("user-data-dir="+pathUri);
        //options.addExtensions(new File(userDir+"//doc//Tampermonkey_v4.15.crx"));
        ChromeDriver webDriver = new ChromeDriver(options);
//        System.setProperty("webdriver.gecko.driver", "F://wb//geckodriver.exe");
//        WebDriver webDriver = new FirefoxDriver();
        webDriver.manage().window().maximize();
//        webDriver.manage().deleteAllCookies();
//        new FlashPolicyHelper(webDriver).addSite("http://www.yy.com:80").addSite("http://www.yy.com:80");

        //String url = "http://www.yy.com/98187265/98187265";
        webDriver.get(baseUri); //
        // 与浏览器同步非常重要，必须等待浏览器加载完毕
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//        WebElement allowButton=webDriver.findElement(By.cssSelector("#coiPage-1 > div.coi-banner__page-footer > button.coi-banner__accept.coi-banner__accept--fixed-margin"));
//         if(allowButton.isDisplayed()){
//             allowButton.click();
//         }
        login(webDriver,username,password);
         //去查询价格
//        WebElement priceLink=webDriver.findElement(By.cssSelector(" #ign-header > div.ign-header__container > div:nth-child(4) > div > a"));
//        if(priceLink.isDisplayed()){
//            priceLink.click();
//        }


    }

    private void price( ChromeDriver webDriver){
        click(webDriver,"#ign-header > div.ign-header__container > div.ign-header__primary.ign-header__primary--account.ign-header__primary--icon > div > a");


    }

    private void login( ChromeDriver webDriver,String username,String password){
        click(webDriver,"#ign-header > div.ign-header__container > div.ign-header__primary.ign-header__primary--account.ign-header__primary--icon > div > a");

        setValue(webDriver,"#usernameInput",username);
        setValue(webDriver,"#passwordInput",password);
        click(webDriver,"#login-form > button");



    }
    private void click(ChromeDriver webDriver,String cssSelector){
        WebElement input=webDriver.findElement(By.cssSelector(cssSelector));
        if(input.isDisplayed()){
            input.click();
            webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }
    }

    private void setValue(ChromeDriver webDriver,String cssSelector,String value){
        WebElement input=webDriver.findElement(By.cssSelector(cssSelector));
        if(input.isDisplayed()){
            input.sendKeys(value);
        }
    }

    public static void main(String[] args) throws InterruptedException {
       // encodeURIComponent
        String userDir = System.getProperty("user.dir");
        //String classesPath=Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);
       System.out.println(userDir);

        // String home = userDir.substring(0, userDir.lastIndexOf('/'));

        //System.out.println("".equals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzIjoyLCJ0IjoidyIsImQiOiJqdjVoTGtyMnh0UGRuVE9PMUpjN0YwZGE5UGlyeUI5R3R3NGlTaTlQZ3pIaWh3N25EOVpNbXpuN1ZvL0RzalRNSnNwUjhCTUVLVTNNbVFKczl3SnQvSkFFTHJ0OER6M0RzWWI4OHk2ZHZGYldwQUZXdnVibWQvTGY4R2VaenZBTDhJR1I3MTNmdkpycSt1U3ExVlRPWHc3WW01a3hxZk1XNFViOHhydzMvVXFabWpVVlU4aWhTTXdodEE9PU5VMWZ5c0diTlliQ3E2dkEiLCJsIjoiaHR0cHM6Ly9uZXdhc3NldHMuaGNhcHRjaGEuY29tL2MvMmZmZGI2YTciLCJlIjoxNjQ3MTM2NTk5fQ.IMxKdsLL8gEWY1jAdvS-9rTa4l1b0zvzc6jOyWbeMoQ"));
        new SeleniumUtil().loginYY("CHANGSHUFENGXIN","Zhuoyang20170223");
       // SeleniumUtil.loginYY("2085734124","ahqo4304");
       // SeleniumUtil.loginYY("2085741302","wdjm6414");
        //webDriver.get("http://www.yy.com/98187265/98187265");



    }
}
